<?php


class TagLibBlog extends TagLib {
	protected $tags = array(
			'new_post' => array('attr'=>'limit','close'=>0),
			'hot_post' => array('attr'=>'limit','close'=>0),
            'new_cmts' => array('attr'=>'limit','close'=>0),
            'tags' => array('attr'=>'limit','close'=>0),

		);

	public function _new_post($attr, $content){
		$tag    = $this->parseXmlAttr($attr,'new_post');
        $limit   =   $tag['limit'];
		$str = <<<STR
<?php

\$_new_list = M('Post')->order('add_time desc')->where('is_del = 0')->limit($limit)->field('id,title')->select();
foreach(\$_new_list as \$v){
	\$str = '<li>';
	\$str .= "<a href=" . U("/\$v[id]") .">";
	\$str .= \$v['title'];
	\$str .= '</a></li>';
	echo \$str;
}

?>
STR;
		return $str;
	}

	public function _hot_post(){
		$tag    = $this->parseXmlAttr($attr,'hot_post');
		$limit   =   $tag['limit'];
		$str = <<<STR
<?php

\$_hot_list = M('Post')->order('click desc')->limit($limit)->field('id,title')->select();
foreach(\$_hot_list as \$v){
\$str = '<li>';
\$str .= "<a href=" . U("/\$v[id]") .">";
\$str .= \$v['title'];
\$str .= '</a></li>';
echo \$str;
}

?>
STR;
		return $str;

	}
    
    
    public function _new_cmts(){
        $tag    = $this->parseXmlAttr($attr,'new_cmts');
		$limit   =   $tag['limit'];
		$str = <<<STR
<?php

\$_new_cmts_list = M('Comment')->where('parent_id = 0 and is_verify = 1')->order('add_time desc')->limit($limit)->field('id,content')->select();
foreach(\$_new_cmts_list as \$v){
\$str = '<li>';
\$str .= "<a href=" . U("/\$v[id]") .">";
\$str .= msubstr(\$v['content'],20);
\$str .= '</a></li>';
echo \$str;
}

?>
STR;
		return $str;
    
    }
    
    public function _tags($attr, $content){        
        $tag = $this->parseXmlAttr($attr,'tags');
		$limit = $tag['limit'];
        $str =<<<EOT
      
<?php

\$sql = "SELECT count(*) as num,t_id FROM `blg_tag_post` group by t_id order by  num desc limit {$limit}";
\$_tags = M()->query(\$sql);
\$db = M('Tags');
foreach(\$_tags as \$v){
\$v['name'] = \$db->where('id = ' .\$v[t_id])->getField('tag_name');
\$str = "<li><a href='#'><span class='category'>\$v[name]</span><span class='number'>\$v[num]</span></a></li> <li><span class='widget-border'></span></li>";
echo \$str;
}

unset(\$sql);
unset(\$_tags);
unset(\$str);
unset(\$db);
?>      
        
EOT;
        return $str;
    }
        

}